package com.example.pointxytest.widget.popup;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.pointxytest.R;
import com.example.pointxytest.widget.OnDialogClick;

/**
 * FileName: PopMenu
 * Author: huangyuguang
 * Date: 2022/5/6
 * Description: 菜单
 */
public class PopMenu extends PopupWindow implements View.OnClickListener{

    private TextView tvEmail;
    private OnDialogClick onDialogClick;

    public PopMenu(Context context,String email) {
        super(context);

        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setOutsideTouchable(true);
        setFocusable(true);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View contentView = LayoutInflater.from(context).inflate(R.layout.pop_menu_tuya,null, false);
        setContentView(contentView);
        setAnimationStyle(R.style.popwin_anim);

        tvEmail = getContentView().findViewById(R.id.tv_email);
        tvEmail.setText(email);
        getContentView().findViewById(R.id.iv_fix_email).setOnClickListener(this);
        getContentView().findViewById(R.id.tv_save).setOnClickListener(this);
    }

    public void setEmail(String email){
        tvEmail.setText(email);
    }

    public String getEmail(){
        return tvEmail.getText().toString();
    }

    public void setOnDialogClick(OnDialogClick onDialogClick) {
        this.onDialogClick = onDialogClick;
    }

    private void onClickResult(int i){
        if(onDialogClick != null){
            onDialogClick.result(i,"");
        }
    }

    /**
     *  在android7.0上，如果不主动约束PopuWindow的大小，比如，设置布局大小为 MATCH_PARENT,那么PopuWindow会变得尽可能大，以至于 view下方无空间完全显示PopuWindow，而且view又无法向上滚动，此时PopuWindow会主动上移位置，直到可以显示完全。
     *　解决办法：主动约束PopuWindow的内容大小，重写showAsDropDown方法：
     * @param anchor
     */
    @Override
    public void showAsDropDown(View anchor) {
//        if(Build.VERSION.SDK_INT >= 24){
//            Rect visibleFrame = new Rect();
//            anchor.getGlobalVisibleRect(visibleFrame);
//            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
//            setHeight(height);
//        }
        //测量view 注意这里，如果没有测量  ，下面的popupHeight高度为-2  ,因为LinearLayout.LayoutParams.WRAP_CONTENT这句自适应造成的
        getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = getContentView().getMeasuredWidth();    //  获取测量后的宽度
        int popupHeight = getContentView().getMeasuredHeight();  //获取测量后的高度
        int[] location = new int[2];
        // 获得位置 这里的v是目标控件，就是你要放在这个v的上面还是下面
        anchor.getLocationOnScreen(location);
        //这里就可自定义在上方和下方了 ，这种方式是为了确定在某个位置，某个控件的左边，右边，上边，下边都可以
        showAtLocation(anchor, Gravity.NO_GRAVITY, (location[0] + anchor.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight - ConvertUtils.dp2px(20));
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id){
            case R.id.iv_fix_email:
                onClickResult(0);
                break;
            case R.id.tv_save:
                if(RegexUtils.isEmail(getEmail())){
                    onClickResult(1);
                    dismiss();
                }else {
                    ToastUtils.showLong("请输入正确的邮箱地址");
                }
                break;
        }
    }
}
